<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns the difference between two DateTime objects</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.datetimeinterface.html">DateTimeInterface</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.format.html">DateTime::format</a></div>
 <div class="up"><a href="class.datetimeinterface.html">DateTimeInterface</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="datetime.diff" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTime::diff</h1>
  <h1 class="refname">DateTimeImmutable::diff</h1>
  <h1 class="refname">DateTimeInterface::diff</h1>
  <h1 class="refname">date_diff</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">DateTime::diff</span> -- <span class="refname">DateTimeImmutable::diff</span> -- <span class="refname">DateTimeInterface::diff</span> -- <span class="refname">date_diff</span> &mdash; <span class="dc-title">Returns the difference between two DateTime objects</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetime.diff-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTime::diff</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span>
  ] )</div>

  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTimeImmutable::diff</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span>
  ] )</div>

  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTimeInterface::diff</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span>
  ] )</div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><a href="function.date-diff.html" class="methodname">date_diff</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime1</code></span>
   , <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Returns the difference between two <a href="class.datetimeinterface.html" class="classname">DateTimeInterface</a>
   objects.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetime.diff-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">datetime</code></em></dt>

    <dd>

     <p class="para">
      The date to compare to.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">absolute</code></em></dt>

    <dd>

     <p class="para">
      Should the interval be forced to be positive?
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetime.diff-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The <a href="class.dateinterval.html" class="classname">DateInterval</a> object representing the
   difference between the two dates 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetime.diff-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2207">
   <p><strong>Example #1 <span class="function"><strong>DateTime::diff()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$datetime1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$datetime2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-13'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$datetime1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$datetime2</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%R%a&nbsp;days'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$datetime1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$datetime2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-13'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$datetime1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$datetime2</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%R%a&nbsp;days'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
+2 days
</pre></div>
   </div>
  </div>
  <div class="example" id="example-2208">
   <p><strong>Example #2 <a href="class.datetime.html" class="classname">DateTime</a> object comparison</strong></p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     As of PHP 5.2.2, DateTime objects can be compared using
     <a href="language.operators.comparison.html" class="link">comparison operators</a>.
    </p>
   </p></blockquote>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"tomorrow"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程会输出：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(true)
bool(false)
</pre></div>
  </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetime.diff-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="dateinterval.format.html" class="function" rel="rdfs-seeAlso">DateInterval::format()</a> - Formats the interval</span></li>
   <li class="member"><span class="function"><a href="datetime.add.html" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - Adds an amount of days, months, years, hours, minutes and seconds to a
   DateTime object</span></li>
   <li class="member"><span class="function"><a href="datetime.sub.html" class="function" rel="rdfs-seeAlso">DateTime::sub()</a> - Subtracts an amount of days, months, years, hours, minutes and seconds from
   a DateTime object</span></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.datetimeinterface.html">DateTimeInterface</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.format.html">DateTime::format</a></div>
 <div class="up"><a href="class.datetimeinterface.html">DateTimeInterface</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
